package com.klb.action.catalog;

import com.klb.dao.UserDao;
import com.klb.service.catalog.impl.LoginServiceCatalogImpl;
import com.klb.tools.PostValue;
import com.klb.tools.Tools;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by fujia on 2016/5/27.
 */
public class LoginActionCatalog extends ActionSupport {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String telephone;
    private String password;
    private HashMap<String,Object> data;
    
    private LoginServiceCatalogImpl loginServiceCatalog;

    private UserDao userDao;

    

    public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}


	public void setLoginServiceCatalog(LoginServiceCatalogImpl loginServiceCatalog) {
		this.loginServiceCatalog = loginServiceCatalog;
	}

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public String execute() throws Exception {

        if (this.loginServiceCatalog.loginIsSuccess(this.telephone, this.password))
            return SUCCESS;

        return INPUT;
    }


    public String jsonForLogin(){
        JSONObject jsonObject = PostValue.getJsonObject();

        if (!Tools.objectIsNullOrNot(jsonObject)){
            String telephoneP = (String) jsonObject.get("telephone");
            String passwordP = (String) jsonObject.get("password");

            this.data = new HashMap<>();

            if (this.loginServiceCatalog.loginIsSuccess(telephoneP, passwordP)){
                int userId = this.userDao.getUserByTelephone(telephoneP).getUserId();

                this.data.put("userId", userId);
                this.data.put("success", true);
            } else {
                this.data.put("success", false);
            }

        } else {
            this.data = null;
        }

        return SUCCESS;
    }

    public Map<String, Object> getData() {
        return data;
    }

}
